#include "dialogfrequence.h"
#include "ui_DialogFrequence.h"
#include "appconst.h"
#include "TStringUtil.h"
#include <QStyledItemDelegate>
#include "excelfile.h"
#include "appconst.h"
#include <QFileDialog>
#include <QMessageBox>


DialogFrequence::DialogFrequence(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::DialogFrequence)
{
    ui->setupUi(this);

//    QStringList header;
//    header << tr("Num") << tr("Name") << tr("Memo");
//    ui->tableWidget->setColumnCount(header.size());
//    ui->tableWidget->setHorizontalHeaderLabels(header);

    fillNumCombo();
}

DialogFrequence::~DialogFrequence()
{
    delete ui;
}

void DialogFrequence::on_pushButtonAdd_clicked()
{
    QString num = ui->comboBoxNum->currentText();
    QString name = ui->lineEditName->text();
    QString memo = ui->textEditMemo->toPlainText();

    if (num.length() != 3)
    {
        QMessageBox::warning(this, "验证失败.", "序号取值必须在[001,999]之间.");
        return;
    }

    if (name.isEmpty())
    {
        QMessageBox::warning(this, "验证失败.", "名称必填.");
        return;
    }

    if (m_freqList.contains(num.toStdString()))
    {
		QMessageBox::warning(this, "验证失败.", "序号已经存在.");
        return;
    }

    tv::Frequence freq;
    freq.num = num.toStdString();
    freq.name = name.toStdString();
    freq.memo = memo.toStdString();

    m_freqList.push_back(freq);
    appendFreq(freq);
}

void DialogFrequence::on_pushButtonDelete_clicked()
{
    int row = ui->tableWidget->currentRow();
    if (row < 0 || row >= ui->tableWidget->rowCount())
    {
        return;
    }

    m_freqList.remove(row);
    ui->tableWidget->removeRow(row);
}

void DialogFrequence::on_pushButtonEdit_clicked()
{
    int row = ui->tableWidget->currentRow();
    updateFreq(row);
}

void DialogFrequence::on_pushButtonImport_clicked()
{
	QFileDialog fileDialog(this);
	fileDialog.setFileMode(QFileDialog::ExistingFile);
	if (fileDialog.exec() != QDialog::Accepted)
	{
		return;
	}

	QStringList fileList = fileDialog.selectedFiles();
	if (fileList.empty())
	{
		return;
	}

	std::string filepath = fileList[0].toStdString();
	ExcelFile::load(filepath, m_freqList);

	refreshTable();
}

void DialogFrequence::on_pushButtonExport_clicked()
{
	QString dir = AppConst::getQAppDir();
	QString filename = QFileDialog::getSaveFileName(this, tr("Save File"), dir,
		tr("Excel file (*.xlsx)"));

	std::string filepath = filename.toStdString();
	ExcelFile::save(m_freqList, filepath);
}

void DialogFrequence::setFreqList(tv::FrequenceList& freqList)
{
    m_freqList = freqList;

	refreshTable();
}

tv::FrequenceList& DialogFrequence::getFreqList()
{
    return m_freqList;
}

void DialogFrequence::setFreq(int row, const tv::Frequence& freq)
{
    int col = 0;

    {
        QTableWidgetItem* item = new QTableWidgetItem();
        item->setText(freq.num.c_str());

        Qt::ItemFlags flags = item->flags();
        flags &= ~Qt::ItemIsEditable;
        item->setFlags(flags);

        item->setData(kNumRole, freq.num.c_str());

        ui->tableWidget->setItem(row, col ++, item);
    }

    {
        QTableWidgetItem* item = new QTableWidgetItem();
        item->setText(freq.name.c_str());
        Qt::ItemFlags flags = item->flags();
        flags &= ~Qt::ItemIsEditable;
        item->setFlags(flags);
        ui->tableWidget->setItem(row, col ++, item);
    }


    {
        QTableWidgetItem* item = new QTableWidgetItem();
        item->setText(freq.memo.c_str());
        Qt::ItemFlags flags = item->flags();
        flags &= ~Qt::ItemIsEditable;
        item->setFlags(flags);
        ui->tableWidget->setItem(row, col ++, item);
    }
}

void DialogFrequence::fillNumCombo()
{
    QStyledItemDelegate* itemDelegate = new QStyledItemDelegate();
    ui->comboBoxNum->setItemDelegate(itemDelegate);
    ui->comboBoxNum->setStyleSheet("QComboBox QAbstractItemView::item {min-height: 28px;}");

    ui->comboBoxNum->setValidator(new QIntValidator());

    for (int i = 1; i <= AppConst::MAX_FREQ_NUM; ++ i)
    {
        std::string num = comn::StringUtil::format("%03d", i);
        ui->comboBoxNum->addItem(num.c_str());
    }
}

void DialogFrequence::appendFreq(const tv::Frequence& freq)
{
    int rowCount = ui->tableWidget->rowCount();
    ui->tableWidget->insertRow(rowCount);
    setFreq(rowCount, freq);
}


void DialogFrequence::on_tableWidget_currentItemChanged(QTableWidgetItem *current, QTableWidgetItem *previous)
{
	if (!current)
	{
		return;
	}

    int row = current->row();
    showFreq(row);
}

void DialogFrequence::showFreq(int row)
{
    if (row < 0 || row >= (int)m_freqList.size())
    {
        return;
    }

    showFreq(m_freqList[row]);
}

void DialogFrequence::showFreq(const tv::Frequence& freq)
{
    ui->comboBoxNum->setCurrentText(freq.num.c_str());
    ui->lineEditName->setText(freq.name.c_str());
    ui->textEditMemo->setPlainText(freq.memo.c_str());
}

void DialogFrequence::updateFreq(int row)
{
    if (row < 0 || row >= (int)m_freqList.size())
    {
        return;
    }

    tv::Frequence& freq = m_freqList[row];
    freq.num = ui->comboBoxNum->currentText().toStdString();
    freq.name = ui->lineEditName->text().toStdString();
    freq.memo = ui->textEditMemo->toPlainText().toStdString();

    setFreq(row, freq);
}


void DialogFrequence::refreshTable()
{
    ui->tableWidget->setRowCount(0);
	ui->tableWidget->clearContents();

	for (size_t i = 0; i < m_freqList.size(); ++i)
	{
		const tv::Frequence& freq = m_freqList[i];

		appendFreq(freq);
	}
}
